#!/usr/bin/env python
#-*- coding:utf8 -*-
import web
import hashlib
from app.models.model import Model
from app.models.traffic import Traffic

class SiteTraffic(Model):
    Traffic = Traffic()
    table = 'sites_traffic'

    def create(self,tid, data):
        #判断是否存在临时数据
        if self.get_by('sid = %s and status = 2' % data['sid']):
            sql = '''update %s set
                hit_byte = hit_byte + %s,
                hit_count = hit_count + %s,
                total_byte = total_byte + %s,
                total_count = total_count + %s
                where sid = %s and status = 2
            ''' % (self.table,data['hit_byte'],data['hit_count'],data['total_byte'],data['total_count'],data['sid'])

            try:
                self.query(sql)
            except:
                return False
            else:
                return self.Traffic.update_by_status(tid)
        else:
            try:
                self.insert(
                    sid         = data['sid'],
                    hit_byte    = data['hit_byte'],
                    hit_count   = data['hit_count'],
                    total_byte  = data['total_byte'],
                    total_count = data['total_count'],
                    status      = 2
                )
            except:
                return False
            else:
                return self.Traffic.update_by_status(tid)

    def update_by_status(self,_map):
        return self.save(_map, status = 1)
